//
//  UserCancelVC.m
//  Project_IOS
//
//  Created by topnet on 2022/3/18.
//

#import "UserCancelVC.h"

#import "LoginNumberView.h"
#import "CodeNumberView.h"
#import "CodeMan.h"
#import <Masonry/Masonry.h>
#import "LoginTools.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import <MJExtension/MJExtension.h>
@interface UserCancelVC ()
@property (nonatomic, strong) CodeMan *codeMan;
@property (nonatomic, strong) CodeNumberView *numberView;
@property (nonatomic, strong) LoginNumberView *pwView;
@property (nonatomic, strong) UILabel *accountLbl;
@end

@implementation UserCancelVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleStr = self.isBind ? @"绑定账号" : @"注销账号";
    
    self.numberView = [[CodeNumberView alloc] init];
    [self.numberView.moreBtn addTarget:self action:@selector(sendCodeClick) forControlEvents:UIControlEventTouchUpInside];
    self.accountLbl.text = self.isBind ? self.mobile : [LoginTools currentLoginUser].nickname;
    [self.contentView addSubview:self.accountLbl];
    [self.accountLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(110);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(311, 25));
    }];
    
    [self.contentView addSubview:self.numberView];
    [self.numberView.textField addTarget:self action:@selector(textfieldContentChange:) forControlEvents:UIControlEventEditingChanged];
    [self.numberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.accountLbl.mas_bottom).offset(10);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(311, 52));
    }];
    
    self.pwView = [[LoginNumberView alloc] initPhone:false];
    self.pwView.hidden = !self.isBind;
    [self.pwView.textField addTarget:self action:@selector(textfieldContentChange:) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:self.pwView];
    [self.pwView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.numberView.mas_bottom).offset(20);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(311, 52));
    }];
    
    self.buttonView.avable = false;
    [self.contentView addSubview:self.buttonView];
    [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(311, 48));
            make.centerX.mas_equalTo(0);
        
        make.top.mas_equalTo(self.isBind ? self.pwView.mas_bottom : self.numberView.mas_bottom).offset(30);
    }];
    self.buttonView.btnStr = @"确认";
    [self.buttonView.button addTarget:self action:@selector(loginBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self sendCodeClick];
}


- (void)textfieldContentChange:(UITextField *)field {
    if (self.numberView.textField.text.length > 0) {
        if (self.isBind) {
            if ( self.pwView.textField.text != nil && self.pwView.textField.text.length > 0) {
                self.buttonView.avable = true;
            }else {
                self.buttonView.avable = false;
            }
        }else {
            self.buttonView.avable = true;
        }
    }else{
        self.buttonView.avable = false;
    }
}

- (void)sendCodeClick {
//    MJWeakSelf
//    [self.codeMan sendCode: self.isBind ? self.mobile : [LoginTools currentLoginUser].mobile complete:^(BOOL success) {
//        if (success) {
//            [weakSelf.numberView startCount:60];
//        }
//    }];
}

- (void)loginBtnOnClick {
    
    if (self.numberView.textField.text.length <= 0) {
//        [MBProgressHUD showMessage:@"请输入验证码！"];
        return;
    }
    if (self.isBind) {
        [self requestToBind];
        return;
    }
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"请确认是否注销" preferredStyle:UIAlertControllerStyleAlert];

    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    NSLog(@"点击取消");

    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        [self sureToCancel];

    }]];

    // 由于它是一个控制器 直接modal出来就好了

    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)sureToCancel {
//    MJWeakSelf
//    [self.codeMan commitCode:self.numberView.textField.text mobile:[LoginTools currentLoginUser].mobile complete:^(BOOL success) {
//        [weakSelf requestToCancel];
//    }];
    [self requestToCancel];
//    [MobClick event:@"myCancelClick"];
}

- (void)requestToCancel {
    [LoginTools loginCancel:self.numberView.textField.text success:^{
        [self dismiss];
    }];
}

- (void)requestToBind {
    [LoginTools loginBind:self.mobile captcha: self.numberView.textField.text pwd: self.pwView.textField.text success:^{
        [self dismiss];
    }];
}

- (UILabel *)accountLbl {
    if (!_accountLbl) {
        _accountLbl = [UILabel new];
        _accountLbl.textColor = UIColor.lightGrayColor;
        _accountLbl.font = [UIFont systemFontOfSize:15];
        
    }
    return _accountLbl;
}

- (CodeMan *)codeMan {
    if (!_codeMan) {
        _codeMan = [CodeMan new];
    }
    return _codeMan;
}

@end
